package com.example.nonameaa.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.nonameaa.Common.Result;
import com.example.nonameaa.entity.Blog;
import com.example.nonameaa.entity.User;
import com.example.nonameaa.service.BlogService;
import com.example.nonameaa.service.BlogService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author NoName
 * @since 2024-10-15
 */
@RestController
@RequestMapping("/blog")
public class BlogController {
    @Resource
    private BlogService blogService;

    /**
     *修改信息
     * @param blog
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody Blog blog) {
        blogService.updateById(blog);
        return Result.success();
    }

    /**
     * 删除信息
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {

        blogService.removeById(id);
        return Result.success();
    }

    /**
     * 新增信息
     * @param blog
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Blog blog) {
        blog.setTime(DateUtil.now());
        blogService.save(blog);
        return Result.success();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids) {
        blogService.removeByIds(ids);
        return Result.success();
    }

    /**
     * 查询全部信息
     * @return
     */
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<Blog> userList = blogService.list(new QueryWrapper<Blog>().orderByDesc("id"));
        return Result.success(userList);
    }

    @GetMapping("/selectByPage")
    public Result selectByPage( @RequestParam Integer pageNum,
                                @RequestParam Integer pageSize
                                ) {
        QueryWrapper<Blog> queryWrapper = new QueryWrapper<Blog>().orderByAsc("id");
        Page<Blog> page = blogService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }
}
